package handler

import (
	"chirp-read/cache"
	"chirp-read/models"
	"encoding/json"
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
	"strconv"
	"time"
)

func CarAdd(c *gin.Context) {
	car_name := c.PostForm("car_name")
	car_type := c.PostForm("car_type")
	car_price, _ := strconv.Atoi(c.PostForm("car_price"))
	if car_name == "" || car_type == "" || car_price == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不能为空",
		})
		return
	}
	data := models.Car{
		Model:    gorm.Model{},
		CarName:  car_name,
		CarType:  car_type,
		CarPrice: car_price,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func CarOrderAdd(c *gin.Context) {
	order_code := c.PostForm("order_code")
	pay_type := c.PostForm("pay_type")
	car_id, _ := strconv.Atoi(c.PostForm("car_id"))
	if order_code == "" || pay_type == "" || car_id == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不能为空",
		})
		return
	}
	data := models.CarOrder{
		Model:     gorm.Model{},
		OrderCode: order_code,
		PayType:   pay_type,
		CarId:     car_id,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func CarOrderList(c *gin.Context) {
	/*
		SELECT
		  car_orders.id AS OrderId,
		  car_orders.order_code AS OrderCode,
		  car_orders.pay_type AS ParType,
		  cars.car_name AS CarName,
		  cars.car_type AS CarType,
		  cars.car_price AS CarPrice
		FROM
		  car_orders
		  LEFT JOIN cars ON cars.id = car_orders.car_id
	*/
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "5"))
	offset := (page - 1) * size
	var carOrder []models.CarOrderList
	redis_key := "car_order_list_page_" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Table("car_orders").
			Select("car_orders.id AS OrderId",
				"car_orders.order_code AS OrderCode",
				"car_orders.pay_type AS ParType",
				"cars.car_name AS CarName",
				"cars.car_type AS CarType",
				"cars.car_price AS CarPrice",
			).
			Joins("LEFT JOIN cars ON cars.id = car_orders.car_id").
			Offset(offset).Limit(size).Find(&carOrder)
		jsonData, _ := json.Marshal(carOrder)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &carOrder)
	}
	var total int64
	models.DB.Model(&models.GoodOrder{}).Count(&total)
	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "ok",
		"data":  carOrder,
		"total": total,
	})
}
func CarOrderDetail(c *gin.Context) {
	/*
		SELECT
		  car_orders.id AS OrderId,
		  car_orders.order_code AS OrderCode,
		  car_orders.pay_type AS ParType,
		  cars.car_name AS CarName,
		  cars.car_type AS CarType,
		  cars.car_price AS CarPrice
		FROM
		  car_orders
		  LEFT JOIN cars ON cars.id = car_orders.car_id
	*/
	id := c.Query("id")
	var carOrder models.CarOrderList
	redis_key := "car_order_detail_" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Table("car_orders").
			Select("car_orders.id AS OrderId",
				"car_orders.order_code AS OrderCode",
				"car_orders.pay_type AS ParType",
				"cars.car_name AS CarName",
				"cars.car_type AS CarType",
				"cars.car_price AS CarPrice",
			).
			Joins("LEFT JOIN cars ON cars.id = car_orders.car_id").
			Where("car_orders.id = ?", id).Find(&carOrder)
		jsonData, _ := json.Marshal(carOrder)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &carOrder)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "ok",
		"data": carOrder,
	})
}
